import axios from 'axios';
import i18n from '@/lang';

import { businessStatusCode, httpStatusCode } from './statusCode';
import { Loading } from 'element-ui';

let loading;

const startLoading = () => {
  loading = Loading.service({
    // lock: true,
    text: i18n.t('加载中……'),
    background: 'rgba(0, 0, 0, 0.2)',
    customClass: 'Loading',
    // fullscreen: false,
  });
};
const endLoading = () => {
  loading.close();
};

/**
 * 返回类型
 */
declare interface Result<T = any> {
  code: number;
  msg?: string;
  data?: T;
}

/**
 * 创建axios对象
 * @param fn 扩展回调
 */

const HttpBlacklist = ['/api/reconciliation/file/fileZipUpload']; // 请求黑名单

const createAxios = (reqConf: any, fn?: any) => {
  // 创建axios实例envConf.apiHost
  const service = axios.create({
    timeout: 2 * 60 * 1000, // 请求超时时间
    responseType: reqConf.responseType, // 表示服务器响应的数据类型
  });

  // request 请求拦截器
  service.interceptors.request.use(
    (config: any) => {
      if (config && reqConf.isLoading) {
        startLoading();
      }
      return config;
    },
    (error: any) => {
      return;
    },
  );

  // respone 返回 拦截器
  service.interceptors.response.use(
    (response: any) => {
      fn(response); // 回调 用于后期自定义扩展

      if (response.config.responseType !== 'blob' && response.data.code != 2000) {
        // 业务状态判断
        businessStatusCode(response.data);
      }

      endLoading();
      return response;
    },
    (error: any) => {
      endLoading();
      if (HttpBlacklist.indexOf(error.config.url.split('rs')[1]) == -1) {
        // 回调 用于后期自定义扩展
        if (error.message.includes('timeout')) {
          // 判断请求异常信息中是否含有超时timeout字符串
          // fn();
        }
        if (!error.response) {
          return Promise.reject(error);
        }

        // 网络状态处理
        httpStatusCode(error.response);
        return;
      }
    },
  );

  return service;
};

/**
 * Post请求
 * @param url 【链接】
 * @param params【参数】
 * @param fn【回调】
 */
const requestPost = (url: string, params: any, fn: any, isLoading: boolean) => {
  const reqConf = {
    headers: {
      Accept: 'application/json', // json类型,
      'Content-Type': 'application/json;charset=UTF-8',
    },
    responseType: 'json',
    isLoading,
  };
  const axiosObj = createAxios(reqConf, fn);
  return axiosObj.post<Result>(url, params);
};

/**
 * get请求
 * @param url 【链接】
 * @param params【参数】
 * @param fn【回调】
 */
const requestGet = (url: string, params: any, fn: any, isLoading: boolean) => {
  const reqConf = {
    headers: {
      Accept: 'application/json', // json类型,
      'Content-Type': 'application/json;charset=UTF-8',
    },
    responseType: 'json',
    isLoading,
  };
  const axiosObj = createAxios(reqConf, fn);
  return axiosObj.get<Result>(url, { params: { ...params } });
};

/**
 * Post请求
 * @param url 【链接】
 * @param params【参数】
 * @param token
 * @param fn【回调】
 */
const fileDownload = (url: string, params: FormData, fn: any) => {
  const reqConf: any = {
    headers: {
      Accept: 'application/json',
      'Content-Type': 'application/json;charset=UTF-8',
    },
    responseType: 'blob',
  };

  const axiosObj = createAxios(reqConf, fn);
  return axiosObj.post(url, params);
};

export default {
  get: requestGet,
  post: requestPost,
  download: fileDownload,
};
